package com.chen.tankwar;

import java.awt.*;
import java.awt.event.KeyEvent;

/**
 * Created by jijie.chen on 2017/11/11.
 */
public class Tank {

    //坦克的x坐标
    private int x;
    //坦克的y坐标
    private int y;

    public static final int WIDTH = 30;
    public static final int HEIGHT = 30;

    private boolean bL = false;
    private boolean bR = false;
    private boolean bU = false;
    private boolean bD = false;

    public static final int XSPEED = 10;
    public static final int YSPEED = 10;
    private TankClient tc;
    private Direction tankDirection;
    private Direction gunDirection = Direction.DOWN;

    public Tank(int x, int y, Direction tankDirection, TankClient tc) {
        this.x = x;
        this.y = y;
        this.tankDirection = tankDirection;
        this.tc = tc;
    }

    public void draw(Graphics g) {
        g.setColor(Color.RED);
        g.fillOval(x, y, WIDTH, HEIGHT);
        g.setColor(Color.black);
        switch (gunDirection) {
            //左
            case LEFT:
                g.drawLine(x, y + Tank.HEIGHT / 2, x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2);
                break;
            //右
            case RIGHT:
                g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x + Tank.WIDTH, y + Tank.HEIGHT / 2);
                break;
            //上
            case UP:
                g.drawLine(x + Tank.WIDTH / 2, y  , x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2);
                break;
            //下
            case DOWN:
                g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x + Tank.WIDTH / 2, y + Tank.HEIGHT);
                break;
            //左上
            case LU:
                g.drawLine(x, y, x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2);
                break;
            //左下
            case LD:
                g.drawLine(x, y + Tank.HEIGHT, x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2);
                break;
            //右上
            case RU:
                g.drawLine(x + Tank.WIDTH, y, x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2);
                break;
            //右下
            case RD:
                g.drawLine(x + Tank.WIDTH, y + Tank.HEIGHT, x + Tank.WIDTH /2 , y + Tank.HEIGHT/2);
                break;
            default:
                break;
        }
        move();
    }

    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();
        switch (key) {
            case KeyEvent.VK_CONTROL:
                tc.setMissile(fire());
                break;
            case KeyEvent.VK_LEFT:
                bL = true;
                break;
            case KeyEvent.VK_UP:
                bU = true;
                break;
            case KeyEvent.VK_RIGHT:
                bR = true;
                break;
            case KeyEvent.VK_DOWN:
                bD = true;
                break;
            default:
                break;
        }
        locationDirection();
    }

    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();
        switch (key) {

            case KeyEvent.VK_LEFT:
                bL = false;
                break;
            case KeyEvent.VK_UP:
                bU = false;
                break;
            case KeyEvent.VK_RIGHT:
                bR = false;
                break;
            case KeyEvent.VK_DOWN:
                bD = false;
                break;
            default:
                break;
        }
        locationDirection();
    }


    private void locationDirection() {
        //上
        if (!bL && bU && !bR && !bD) {
            tankDirection = Direction.UP;
        }
        //下
        else if (!bL && !bU && !bR && bD) {
            tankDirection = Direction.DOWN;
        }
        //左
        else if (bL && !bU && !bR && !bD) {
            tankDirection = Direction.LEFT;
        }
        //右
        else if (!bL && !bU && bR && !bD) {
            tankDirection = Direction.RIGHT;
        }
        //左上
        else if (bL && bU && !bR && !bD) {
            tankDirection = Direction.LU;
        }
        //左下
        else if (bL && !bU && !bR && bD) {
            tankDirection = Direction.LD;
        }
        //右上
        else if (!bL && bU && bR && !bD) {
            tankDirection = Direction.RU;
        }
        //右下
        else if (!bL && !bU && bR && bD) {
            tankDirection = Direction.RD;
        }
        //STOP
        else if (!bL && !bU && !bR && !bD) {
            tankDirection = Direction.STOP;
        }
    }

    /**
     * 移动
     */
    private void move() {
        switch (tankDirection) {
            //左
            case LEFT:
                this.x -= XSPEED;
                break;
            //右
            case RIGHT:
                this.x += XSPEED;
                break;
            //上
            case UP:
                this.y -= YSPEED;
                break;
            //下
            case DOWN:
                this.y += YSPEED;
                break;
            //左上
            case LU:
                this.x -= XSPEED;
                this.y -= YSPEED;
                break;
            //左下
            case LD:
                this.x -= XSPEED;
                this.y += YSPEED;
                break;
            //右上
            case RU:
                this.x += XSPEED;
                this.y -= YSPEED;
                break;
            //右下
            case RD:
                this.x += XSPEED;
                this.y += YSPEED;
                break;
            default:
                break;
        }
        if( tankDirection != Direction.STOP ) {
            gunDirection = tankDirection;
        }
    }

    //坦克发出了已发炮弹
    public Missile fire() {
        int mx = this.getX() + WIDTH / 2 - Missile.WIDTH / 2;
        int my = this.getY() + HEIGHT / 2 - Missile.HEIGHT / 2;
        Missile m = new Missile(mx, my, gunDirection);
        return m;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public Direction getTankDirection() {
        return tankDirection;
    }

    public void setTankDirection(Direction tankDirection) {
        this.tankDirection = tankDirection;
    }
}
